package com.example.test.controller;

import com.example.test.service.CompletableFutureService;
import com.example.test.model.OrderRequest;
import com.example.test.model.BatchOrderRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.Map;
import java.util.HashMap;

@RestController
@RequestMapping("/api/completable-future")
public class CompletableFutureController {

    @Autowired
    private CompletableFutureService completableFutureService;

    /**
     * 演示1：基本异步执行
     */
    @GetMapping("/basic-demo")
    public CompletableFuture<Map<String, Object>> basicDemo() {
        return completableFutureService.basicAsyncDemo()
            .thenApply(result -> {
                Map<String, Object> response = new HashMap<>();
                response.put("success", true);
                response.put("message", "基本异步执行演示完成");
                response.put("result", result);
                return response;
            });
    }

    /**
     * 演示2：链式调用
     */
    @GetMapping("/chain-demo")
    public CompletableFuture<Map<String, Object>> chainDemo() {
        return completableFutureService.chainDemo()
            .thenApply(result -> {
                Map<String, Object> response = new HashMap<>();
                response.put("success", true);
                response.put("message", "链式调用演示完成");
                response.put("result", result);
                return response;
            });
    }

    /**
     * 演示3：组合多个异步任务
     */
    @GetMapping("/combine-demo")
    public CompletableFuture<Map<String, Object>> combineDemo() {
        return completableFutureService.combineDemo()
            .thenApply(result -> {
                Map<String, Object> response = new HashMap<>();
                response.put("success", true);
                response.put("message", "组合异步任务演示完成");
                response.put("result", result);
                return response;
            });
    }

    /**
     * 演示4：异常处理
     */
    @GetMapping("/exception-demo")
    public CompletableFuture<Map<String, Object>> exceptionDemo() {
        return completableFutureService.exceptionHandlingDemo()
            .thenApply(result -> {
                Map<String, Object> response = new HashMap<>();
                response.put("success", true);
                response.put("message", "异常处理演示完成");
                response.put("result", result);
                return response;
            });
    }

    /**
     * 演示5：超时处理
     */
    @GetMapping("/timeout-demo")
    public CompletableFuture<Map<String, Object>> timeoutDemo() {
        return completableFutureService.timeoutDemo()
            .thenApply(result -> {
                Map<String, Object> response = new HashMap<>();
                response.put("success", true);
                response.put("message", "超时处理演示完成");
                response.put("result", result);
                return response;
            });
    }

    /**
     * 演示6：实际订单处理流程
     */
    @PostMapping("/order-processing")
    public CompletableFuture<Map<String, Object>> orderProcessing(@RequestBody OrderRequest orderRequest) {
        return completableFutureService.processOrder(orderRequest)
            .thenApply(result -> {
                Map<String, Object> response = new HashMap<>();
                response.put("success", result.isSuccess());
                response.put("message", result.getMessage());
                response.put("orderId", orderRequest.getOrderId());
                return response;
            });
    }

    /**
     * 批量处理订单
     */
    @PostMapping("/batch-order-processing")
    public CompletableFuture<Map<String, Object>> batchOrderProcessing(@RequestBody BatchOrderRequest request) {
        return completableFutureService.batchProcessOrders(request.getOrders())
            .thenApply(results -> {
                Map<String, Object> response = new HashMap<>();
                response.put("success", true);
                response.put("message", "批量订单处理完成");
                response.put("results", results);
                response.put("totalOrders", request.getOrders().size());
                response.put("successCount", results.stream().filter(r -> r.isSuccess()).count());
                response.put("failureCount", results.stream().filter(r -> !r.isSuccess()).count());
                return response;
            });
    }
} 